package hypeerweb;
import java.util.*;

import proxyCommand.ObjectDB;

import visitor.*;

public class ConnectionsAdder extends ConnectionsModifier {
	public ConnectionsAdder() { }
	
	public void modifyConnection(Node target, NodeDelegate connection, Node modifyingNode, Node replacingNode) {
		if(connection != null) {
			connection.add(target);
		}
	}
	
	public Node getModifyingNode(Node startingNode) {
		//System.out.println(HyPeerWeb.getSingleton());
		return Range.getNodeRange(startingNode).getUpperBound();
	}
	
	public ArrayList<NodeDelegate> getAllConnections(Node target, Node deletionNode) {
		if(target instanceof NodeProxy) {
			//System.out.println(((NodeProxy)target).globalObjectId);
		}
		return target.getConnections().getAllConnections();
	}
	
	public void setWebId(Node node, Node modifyingNode, Node replacingNode) {
        int id = (int) Math.pow(2, modifyingNode.getHeight()) + modifyingNode.getWebId().getValue();
        node.setWebId(new WebId(id));
        if(node instanceof NodeProxy) {
        	((NodeProxy) node).changeWebId(new WebId(id));
        }
	}
	
	public void modifyConnections(Node target, Node modifyingNode, Node replacingNode) {
        target.setConnections(modifyingNode.getConnections().getChildConnections(target));
	}
	
	public Node finalizeConnections(Node target, Node modifyingNode, Node replacingNode) {
        modifyingNode.removeUpPointers();
        
        //is cap node
        if (target.getFold() != null && target.getFold().getWebId().getValue() == 0) {
        	BroadcastShiftNeighbors broadcast = new BroadcastShiftNeighbors();
        	Parameters p = new Parameters();
        	p.set("shift", "right");
        	broadcast.visit(target, p);
        }

        return null;
	}
	
	public void refreshConnections(Node modifyingNode) {}
}
